package com.benshanyang.transformer;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yk.transformer.viewpager2.AlphaPageTransformer;
import com.yk.transformer.viewpager2.DepthPageTransformer;
import com.yk.transformer.viewpager2.RotateDownPageTransformer;
import com.yk.transformer.viewpager2.RotateUpPageTransformer;
import com.yk.transformer.viewpager2.RotateYTransformer;
import com.yk.transformer.viewpager2.ScaleInTransformer;
import com.yk.transformer.viewpager2.ZoomOutPageTransformer;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private TextView drawerMenu;
    private RadioGroup radioGroup;
    private RadioGroup vRadioGroup;
    private RadioGroup v2RadioGroup;
    private TextView drawerTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findById();
        init();
    }

    private void findById() {
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        vRadioGroup = (RadioGroup) findViewById(R.id.v_radio_group);
        v2RadioGroup = (RadioGroup) findViewById(R.id.v2_radio_group);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerMenu = (TextView) findViewById(R.id.tv_menu);
        drawerTitle = (TextView) findViewById(R.id.tv_drawer_title);
    }

    private void init() {

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_container, new ViewPagerFragment()).commitAllowingStateLoss();

        drawerMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isOpen = drawerLayout.isDrawerVisible(Gravity.LEFT);
                if (isOpen) {
                    drawerLayout.closeDrawer(Gravity.LEFT);
                } else {
                    drawerLayout.openDrawer(Gravity.LEFT);
                }
            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                ((RadioButton) vRadioGroup.getChildAt(0)).setChecked(true);
                ((RadioButton) v2RadioGroup.getChildAt(0)).setChecked(true);

                FragmentTransaction transaction;
                switch (checkedId) {
                    case R.id.rb_view_pager:
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, new ViewPagerFragment()).commitAllowingStateLoss();

                        drawerTitle.setText("ViewPager 切换动画");
                        vRadioGroup.setVisibility(View.VISIBLE);
                        v2RadioGroup.setVisibility(View.GONE);
                        break;
                    case R.id.rb_view_pager_2:
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPager2Fragment.newInstance(0)).commitAllowingStateLoss();

                        drawerTitle.setText("ViewPager2 切换动画");
                        vRadioGroup.setVisibility(View.GONE);
                        v2RadioGroup.setVisibility(View.VISIBLE);
                        break;
                }
            }
        });

        //ViewPager 动画
        vRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction;
                switch (checkedId) {
                    case R.id.accordion:
                        //0 AccordionTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(0)).commitAllowingStateLoss();
                        break;
                    case R.id.background_to_foreground:
                        //1 BackgroundToForegroundTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(1)).commitAllowingStateLoss();
                        break;
                    case R.id.cube_in:
                        //2 CubeInTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(2)).commitAllowingStateLoss();
                        break;
                    case R.id.cube_out:
                        //3 CubeOutTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(3)).commitAllowingStateLoss();
                        break;
                    case R.id.default_anim:
                        //4 DefaultTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(4)).commitAllowingStateLoss();
                        break;
                    case R.id.depth_page_anim:
                        //5 DepthPageTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(5)).commitAllowingStateLoss();
                        break;
                    case R.id.flip_horizontal:
                        //6 FlipHorizontalTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(6)).commitAllowingStateLoss();
                        break;
                    case R.id.flip_vertical:
                        //7 FlipVerticalTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(7)).commitAllowingStateLoss();
                        break;
                    case R.id.foreground_to_background:
                        //8 ForegroundToBackgroundTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(8)).commitAllowingStateLoss();
                        break;
                    case R.id.rotate_down:
                        //9 RotateDownTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(9)).commitAllowingStateLoss();
                        break;
                    case R.id.rotate_up:
                        //10 RotateUpTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(10)).commitAllowingStateLoss();
                        break;
                    case R.id.scale_in_out:
                        //11 ScaleInOutTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(11)).commitAllowingStateLoss();
                        break;
                    case R.id.stack:
                        //12 StackTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(12)).commitAllowingStateLoss();
                        break;
                    case R.id.tablet:
                        //13 TabletTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(13)).commitAllowingStateLoss();
                        break;
                    case R.id.zoom_in:
                        //14 ZoomInTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(14)).commitAllowingStateLoss();
                        break;
                    case R.id.zoom_out_slide:
                        //15 ZoomOutSlideTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(15)).commitAllowingStateLoss();
                        break;
                    case R.id.zoom_out:
                        //16 ZoomOutTranformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPagerFragment.newInstance(16)).commitAllowingStateLoss();
                        break;
                }
            }
        });

        //ViewPager2 动画
        v2RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction;
                switch (checkedId) {
                    case R.id.scale_in:
                        //ScaleInTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPager2Fragment.newInstance(0)).commitAllowingStateLoss();
                        break;
                    case R.id.alpha_page:
                        //AlphaPageTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPager2Fragment.newInstance(1)).commitAllowingStateLoss();
                        break;
                    case R.id.depth_page:
                        //DepthPageTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPager2Fragment.newInstance(2)).commitAllowingStateLoss();
                        break;
                    case R.id.rotate_down_page:
                        //RotateDownPageTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPager2Fragment.newInstance(3)).commitAllowingStateLoss();
                        break;
                    case R.id.rotate_up_page:
                        //RotateUpPageTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPager2Fragment.newInstance(4)).commitAllowingStateLoss();
                        break;
                    case R.id.rotate_y:
                        //RotateYTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPager2Fragment.newInstance(5)).commitAllowingStateLoss();
                        break;
                    case R.id.zoom_out_page:
                        //ZoomOutPageTransformer
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fl_container, ViewPager2Fragment.newInstance(6)).commitAllowingStateLoss();
                        break;
                }
            }
        });
    }

}